package com.my.study.design.ruleParallel.suppor;

import com.my.study.design.ruleParallel.AbstractRule;
import com.my.study.design.ruleParallel.entity.AgeEntity;

import java.lang.reflect.Field;

/**
 * @author: yidujun
 * @create: 2021/07/14 10:26
 */
public class AgeRule extends AbstractRule<AgeEntity> {

    @Override
    protected AgeEntity convert(Object obj) {
        Class<?> clazz = obj.getClass();
        int age = 0;
        try {
            Field ageField = clazz.getDeclaredField("age");
            ageField.setAccessible(true);
            age = (int) ageField.get(obj);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }

        AgeEntity entity = new AgeEntity();
        entity.setAge(age);
        return entity;
    }

    @Override
    protected boolean executeRule(AgeEntity ageEntity) {
        int age = ageEntity.getAge();
        return age == 5;
    }
}
